package com.code.app.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;

import com.code.app.service.DownLoaderService;
import com.code.app.R;
import com.code.app.base.BaseActivity;
import com.code.app.entities.DownLoadInfo;

public class DownloaderActivity extends BaseActivity {

    private final String TAG = "DownloaderActivity";

    private Messenger serviceMessenger;

    private DownLoaderServiceConnection connection;

    private DownLoaderHandler handler;


    class DownLoaderHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    }

    class DownLoaderServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            serviceMessenger = new Messenger(iBinder);
            Message msg = new Message();
            msg.what = 0;
            msg.replyTo = new Messenger(handler);
            try {
                serviceMessenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_downloader;
    }

    @Override
    public void initFunction(Bundle savedInstanceState) {
        handler = new DownLoaderHandler();
        Intent intent = new Intent(this, DownLoaderService.class);
        connection = new DownLoaderServiceConnection();
        bindService(intent, connection, BIND_AUTO_CREATE);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start:
                for (int i = 0; i < 10; i++) {
                    DownLoadInfo downLoadInfo = new DownLoadInfo();
                    downLoadInfo.setName("今日" + i + "条");
                    downLoadInfo.setId(i);
                    downLoadInfo.setProgress(1);
                    downLoadInfo.setUrl("http://www.baidu.com");
                    downLoadInfo.setDescroption("非常牛逼的更新......");
                    downLoadInfo.setDownLoadState(DownLoadInfo.DownLoadState.START);

                    Message msg = Message.obtain();
                    msg.what = DownLoaderService.TASK_ADD;
                    msg.obj = downLoadInfo;
                    try {
                        serviceMessenger.send(msg);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.add:

                break;
            case R.id.stop:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);
        connection = null;
    }
}
